package cn.yeamin.ms.controller.admin;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.yeamin.ms.controller.support.BaseController;
import cn.yeamin.ms.vo.User;
import lombok.extern.slf4j.Slf4j;

/**
 * @packageName: 包名: cn.yeamin.ms.controller.admin
 * @classDesc: 类功能描述:(登录/退出)
 * @author: Administrator
 * @createTime: 2017/7/24 11:40
 * @version: v1.0
 * @copyright:奕明(上海)影视传媒有限公司
 */
@Controller
@RequestMapping("/admin")
@Slf4j  //Lombok插件集成日志
public class LoginController extends BaseController {

    /**
     * @methodDesc: 方法描述:(跳转到登录页面)
     * @author: 李彤
     * @version: v1.0
     * @copyright:奕明(上海)影视传媒有限公司
     */
    @GetMapping("/login")
    public String toLoginPage() {
        return "admin/login";
    }



    /**
     * @methodDesc: 方法描述:(页面表单提交请求进行登录)
     * @author: 李彤
     * @version: v1.0
     * @copyright:奕明(上海)影视传媒有限公司
     */
    @PostMapping("/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,ModelMap model) {

        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        //如果已经登录,直接跳转到首页,无需重新进入Shiro框架进行认证
        if(null != user) {
        	if(log.isInfoEnabled()) {
        		 log.info("-------------------------【" + user.getUserName() + "】已认证,无需重复认证----------------------");
        	}
        	return "redirect:/admin/index";
        }
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            //登录认证,可能会有异常,必须try起来,登录失败会走catch
            subject.login(token);
            user = (User) subject.getPrincipal();
            return "redirect:/admin/index";
        } catch (AuthenticationException e) {
            //认证失败会抛出AuthenticationException异常
            if (log.isDebugEnabled()) {
                log.debug( e.getMessage());
            }
            model.put("message", e.getMessage());
            return "admin/login";
        } catch (Exception e) {
            //其他异常,服务器超时
            if (log.isDebugEnabled()) {
                log.debug( e.getMessage());
            }
            model.put("message", "服务器异常,请稍后再试!");
            return "admin/login";
        }
    }



    /**
     * @methodDesc: 方法描述:(退出登录)
     * @author: 李彤
     * @version: v1.0
     * @copyright:奕明(上海)影视传媒有限公司
     */
    @GetMapping("/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/admin/login";
    }



}
